$(function() {
	$.ajax({
		type : "POST",
		url : baseURL + "contractTable/contract?yearMonth=1",
		contentType : "application/json",
		success : function(r) {
			if (r.code === 0) {
				var str = "";
				$.each(r.list, function(i, n) {
					if (i == 0) {
						str += "<tr style='background-color:#e5e8f3'>";
					} else {
						str += "<tr>";
					}
					$.each(n, function(j, k) {
						str += "<th>" + k + "</th>";
					})
					str += "</tr>";
				})
				$("#collect").html(str);
				$("th:contains('收费项目')").css({
					'width' : '100%',
					'display' : 'inline-block',
					'padding-top' : '16px'
				});
				$('#dateMonthS').val(r.startMonth);
				$('#dateMonthE').val(r.endMonth);
				$('#dateYearS').val(r.startYear);
				$('#dateYearE').val(r.endYear);

			} else {
				alert(r.msg);
			}
		}
	});
	$('#dateMonthS').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				$('#dateMonthS').val(year + '-' + month);
				vm.q.dateMonthS = year + '-' + month;
				$(this).datepicker('close');
			});
	$('#dateMonthE').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				$('#dateMonthE').val(year + '-' + month);
				vm.q.dateMonthE = year + '-' + month;
				$(this).datepicker('close');
			});
	$('#dateYearS').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				$('#dateYearS').val(year);
				vm.q.dateMonthS = year;
				$(this).datepicker('close');
			});
	$('#dateYearE').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				$('#dateYearE').val(year);
				vm.q.dateMonthE = year;
				$(this).datepicker('close');
			});
});

var vm = new Vue({
	el : '#rrapp',
	data : {
		showList : true,
		q : {
			yearMonth : '1',
			assetIds : '',
			buildIds : '',
			contractNo : '',
			dateMonthE : '',
			dateMonthS : '',
			dateYearE : '',
			dateYearS : ''
		}
	},
	methods : {
		exportExcel : function() {
			if (vm.q.yearMonth == 2) {
				vm.q.dateMonthS = $('#dateYearS').val();
				vm.q.dateMonthE = $('#dateYearE').val();
			} else if (vm.q.yearMonth == 1) {
				vm.q.dateMonthS = $('#dateMonthS').val();
				vm.q.dateMonthE = $('#dateMonthE').val();
			}
			vm.q.item = $(".am-selected-status.am-fl").text();
			location.href = baseURL + "contractTable/contractExport?yearMonth="
					+ vm.q.yearMonth + "&dateMonthE=" + vm.q.dateMonthE
					+ "&dateMonthS=" + vm.q.dateMonthS + "&assetIds="
					+ vm.q.assetIds + "&buildIds=" + vm.q.buildIds
					+ "&contractNo=" + vm.q.contractNo + "&item=" + vm.q.item
					+ "&token=" + token;
		},
		query : function() {
			if (vm.q.yearMonth == 2) {
				vm.q.dateMonthS = $('#dateYearS').val();
				vm.q.dateMonthE = $('#dateYearE').val();
			} else if (vm.q.yearMonth == 1) {
				vm.q.dateMonthS = $('#dateMonthS').val();
				vm.q.dateMonthE = $('#dateMonthE').val();
			}
			vm.q.item = $(".am-selected-status.am-fl").text();
			$.ajax({
				type : "POST",
				url : baseURL + "contractTable/contract?yearMonth="
						+ vm.q.yearMonth + "&dateMonthE=" + vm.q.dateMonthE
						+ "&dateMonthS=" + vm.q.dateMonthS + "&assetIds="
						+ vm.q.assetIds + "&buildIds=" + vm.q.buildIds
						+ "&contractNo=" + vm.q.contractNo + "&item="
						+ vm.q.item,
				contentType : "application/json",
				success : function(r) {
					if (r.code === 0) {
						var str = "<>";
						$.each(r.list, function(i, n) {
							if (i == 0) {
								str += "<tr style='background-color:#e5e8f3'>";
							} else {
								str += "<tr>";
							}
							$.each(n, function(j, k) {
								str += "<th>" + k + "</th>";
							})
							str += "</tr>";
						})
						$("#collect").html(str);
						$("th:contains('收费项目')").css({
							'width' : '100%',
							'display' : 'inline-block',
							'padding-top' : '16px'
						});
					} else {
						alert(r.msg);
					}
				}
			});
		}
	}
});
